package com.zyc.webtest.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by zyc on 2017/4/18.
 */
//@Controller
public class TestController {

    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/login")
    public String  login(Model model){
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("jack", "123");
        currentUser.login(token);
        if(currentUser.hasRole("user")){
            logger.info("用户[]登录认证通过(这里可以进行一些认证通过后的一些系统参数初始化操作)");
            return "redirect:/hello";
        }else{
            model.addAttribute("name", "login");
            return "hello";
        }
    }

    /**
     * 使用@ResponseBody 可以不被thyemleaf解析，当请求为ajax需要加上此注解
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/hello/{id}")
    @ResponseBody
    public String  user(Model model,@PathVariable  String id){
        model.addAttribute("name", "Dear");
        return "hello"+id;
    }

    @GetMapping("/index")
    String index(Model model) {
        model.addAttribute("now", LocalDateTime.now());
        return "redirect:login";
    }

    @GetMapping("/uid")
    @ResponseBody
    String uid(HttpSession session) {
        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        session.setAttribute("uid", uid);
        return uid.toString();
    }

    @RequestMapping(value="/getTest",method= RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getTest(String name){
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("title","hello world");
        map.put("name",name);
        return map;
    }

    @RequestMapping(value="/postTest",method= RequestMethod.POST)
    public Map<String,Object> postTest(String name){
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("title","hello world");
        map.put("name",name);
        return map;
    }

    @RequestMapping(value="/deleteTest/{name}",method= RequestMethod.DELETE)
    public Map<String,String> deleteTest(@PathVariable String name){
        Map<String, String> map = new HashMap<String,String>();
        map.put("title","hello world");
        map.put("name",name);
        return map;
    }

    @RequestMapping(value="/putTest/{name}",method= RequestMethod.PUT)
    public Map<String,String> putTest(@PathVariable String name){
        Map<String, String> map = new HashMap<String,String>();
        map.put("title","hello world");
        map.put("name",name);
        return map;
    }

}
